<template>
  <div class="addUser">
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="用户名"> 
        <el-input v-model="form.name" @focus="clean('nameTip')"></el-input> 
        <div class="tip" id="nameTip"></div>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password" type="password"></el-input>
      </el-form-item>
      <el-form-item label="确认密码">
        <el-input v-model="form.rePassword" type="password" @blur="checkPwd" @focus="clean('pwdTip')"></el-input>
        <div class="tip" id="pwdTip"></div>
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" @click="onSubmit">立即创建</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
 import $ from 'jquery'
 import axios from 'axios'
  export default {
    data() {
      return {
        form: {
          name: '',
          password:'',
          rePassword:''
        }
      }
    },
    methods: {
      onSubmit() {
        if(this.form.name==''){
          $("#nameTip").html('用户名不能为空')
          return
        }
        axios.post('http://localhost:3000', {
          username: this.form.name,
          password: this.form.password
        })
        .then(function (response) {
          console.log(response);
        })
        .catch(function (error) {
          console.log(error);
        });
      },
      checkPwd(){
        if(this.form.password!=this.form.rePassword){
          $("#pwdTip").html('两次密码输入不一致')
          return
        }
      },
      clean(id){
        $(`#${id}`).html(' ')
      }
    }
  }
</script>

<style scoped>
.addUser{
  width: 90%;
  margin: 0 auto;
}
.tip{
  color: brown;
  text-indent: 5px
}
</style>